﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using loginSQL.Pages;
using loginSQL.Util;

namespace loginSQL.Window.CollectionEdit
{
    public partial class CollectionManagement : Form
    {
        private BookCollection collections;
        public CollectionManagement()
        {
            InitializeComponent();
            InitListKoleksi();
            InitInfo();
        }

        private void InitInfo()
        {
            if (list_koleksi.SelectedIndex >= 0)
            {
                string licenseName = collections.BookName[list_koleksi.SelectedIndex];
                teks_nama.Text = (String)licenseName;
                int index = list_koleksi.SelectedIndex;
                string def = collections.GetDefaultBookFilename(teks_nama.Text);
                string cur = collections.GetCurrentBookFilename(teks_nama.Text);
                teks_default.Text = def != null ? def : "";
                teks_current.Text = cur != null ? cur : "";
            }
        }

        private void InitListKoleksi()
        {
            collections = Constants.collections;
            list_koleksi.Items.Clear();
            foreach (String nama in collections.BookName)
            {
                list_koleksi.Items.Add(nama);
            }
            list_koleksi.ClearSelected();
        }

        private void list_koleksi_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitInfo();
        }

        private void teks_nama_TextChanged(object sender, EventArgs e)
        {
            if (list_koleksi.SelectedIndex >= 0)
            {
                collections.ChangeBookName((String)list_koleksi.Items[list_koleksi.SelectedIndex], teks_nama.Text);
                list_koleksi.Items[list_koleksi.SelectedIndex] = teks_nama.Text;
            }
        }

        private void btn_default_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BookData book = BookData.Load(ofd.FileName);
                teks_default.Text = ofd.FileName;
                book.Name = teks_nama.Text;
                book.Save(ofd.FileName);
                /*
                if (book.Name.Equals(teks_nama.Text, StringComparison.OrdinalIgnoreCase))
                {
                 */
                    collections.UpdateDefaultBookPath(ofd.FileName);
                /*
                }
                else
                {
                    MessageBox.Show("Nama buku yang akan dimasukkan berbeda", "Error Buku", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                 */
            }
            else
            {
            }
        }

        private void btn_current_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BookData book = BookData.Load(ofd.FileName);
                teks_current.Text = ofd.FileName;
                book.Name = teks_nama.Text;
                book.Save(ofd.FileName);
                /*
                if (book.Name.Equals(teks_nama.Text, StringComparison.OrdinalIgnoreCase))
                {
                 */
                    collections.UpdateBookPath(ofd.FileName);
                /*
                }
                else
                {
                    MessageBox.Show("Nama buku yang akan dimasukkan berbeda", "Error Buku", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                 */
            }
            else
            {
            }
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            collections.Reset();
            InitListKoleksi();
            InitInfo();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            NewBook nb = new NewBook();
            if (nb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BookData book = new BookData();
                book.Name = nb.name;
                book.Save(nb.filename);
                collections.AddNewBook(nb.filename);
                InitListKoleksi();
                InitInfo();
            }
            else
            {
                nb.Dispose();
            }
        }

        private void btn_remove_Click(object sender, EventArgs e)
        {
            if (list_koleksi.SelectedIndex>=0)
            {
                collections.RemoveBook((string)list_koleksi.Items[list_koleksi.SelectedIndex]);
                InitListKoleksi();
                InitInfo();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            collections.Save(Properties.Resources.collectionsFilename);
            MessageBox.Show("Data berhasil disimpan");
        }
    }
}
